package com.cy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * spring中的拦截器需要实现HandlerInterceptor接口
 */
public class TimeInterceptor implements HandlerInterceptor {
    /*此方法是在@Contrller对象方法执行之前执行*/
    //重写HandlerInterceptor接口里面的preHandle方法
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        LocalDateTime time = LocalDateTime.now(); //获取当前时间
        int hour = time.getHour(); //获取当前时间中的小时
        System.out.println("hour = "+hour);
        if(hour<0 || hour>24) {
            throw new RuntimeException("请在指定时间访问");
        }
        return true; //true  表示放行  false 表示不放行
    }
}
